package com.fei.controller;

import com.fei.bean.BroadcastBean;
import com.fei.bean.FileAudioBean;
import com.fei.bean.UserBean;
import com.fei.service.BroadcastService;
import com.fei.service.FileAudioService;
import com.fei.service.MusicService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BroadcastController {

    @Autowired
    MusicService musicService;
    @Autowired
    BroadcastService broadcastService;
    @Autowired
    FileAudioService fileAudioService;

    @RequestMapping("/user/addBroadcast")
    @ResponseBody
    public FileAudioBean addBroadcast(String songerName){
//        通过歌名查找id
        Integer Music_id = musicService.selectOneSongerNameService(songerName);

        Subject subject = SecurityUtils.getSubject();
        UserBean user = (UserBean) subject.getSession().getAttribute("user");

        BroadcastBean broadcastBean = new BroadcastBean();
        broadcastBean.setMusic(Music_id);
        broadcastBean.setPhone(user.getPhone());

        //判断该用户是否添加到歌单中
        if(broadcastService.selectCountMusicService(broadcastBean)){
//            添加到用户歌单表
            broadcastService.insertBroadcastService(broadcastBean);
//            broadcastBean.getId(),获取添加后的id
            FileAudioBean audio = fileAudioService.selectOneIdBroad(broadcastBean.getId());
            return audio;
        }else {
            //如果已经有了就删除该歌单
            broadcastService.deleteOneMusicIdService(Music_id);
        }

        return null;
    }

}
